package yxk.utils;

import com.huawei.shade.org.joda.time.DateTime;
import com.huawei.shade.org.joda.time.format.DateTimeFormat;
import kd.bos.algo.sql.tree.In;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import scala.Int;
import yxk.task.utils.RequesUtil;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author yss
 * @createTime 2022-12-26 16:54
 * @description
 */
public class DateUtil {

    private static final Log LOG = LogFactory.getLog(DateUtil.class);
    public static String getDateStrFromISO8601Timestamp(String ISOdate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
            Date date = format.parse(ISOdate);
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return df.format(date);
        }catch (Exception e){
            LOG.error("DateUtil.getDateStrFromISO8601Timestamp.e{}",e);
            return null;
        }
    }

    public static String getISO8601Timestamp(String ISOdate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            Date date = format.parse(ISOdate);
            SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return df.format(date);
        }catch (Exception e){
            LOG.error("DateUtil.getDateStrFromISO8601Timestamp.e{}",e);
            return null;
        }
    }


    /**
     * 传入String类型日期(yyyy-MM-dd HH:mm:ss)，返回字符串类型时间（ISO8601标准时间）
     * @param timestamp 传入转换的日期
     * @return
     */
    public static String getISO8601TimestampFromDateStr(Date timestamp){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime ldt = LocalDateTime.parse(format.format(timestamp),dtf1);
        ZoneOffset offset = ZoneOffset.of("+08:00");
        OffsetDateTime date = OffsetDateTime.of(ldt ,offset);
        DateTimeFormatter dtf2 = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");

        return date.format(dtf2);
    }

    public static int getTimeDifference(String start,String end){
        try{
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = format.parse(start);
            Date endDate = format.parse(end);

            Long endTime = endDate.getTime();
            Long startTime = startDate.getTime();
            Long time = endTime - startTime;
            Long num = time / 24 / 60 / 60 / 1000;
            return Integer.parseInt(num.toString())+1;
        }catch (Exception e){
            LOG.error("DateUtil.getTimeDifference.e:{}",e);
            return 0;
        }

    }

    /**
     * 获得当日0点
     */
    public static Date getZero(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }
    /**
     * 获得当日最后时间
     */
    public static Date getEnd(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 获取当年最早时刻
     * @param date
     * @return
     */
    public static Date getFirstTimeOfYear(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, 0);
        return getFirstTimeOfMonth(c.getTime());
    }

    /**
     * 获取当年最晚时刻
     * @param date
     * @return
     */
    public static Date getEndTimeOfYear(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.MONTH, 11);
        return getEndTimeOfMonth(c.getTime());
    }

    /**
     * 获取当月最早时刻
     * @param date
     * @return
     */
    public static Date getFirstTimeOfMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return getFirstTime(c.getTime());
    }

    /**
     * 获取当月最晚时刻
     * @param date
     * @return
     */
    public static Date getEndTimeOfMonth(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 0);
        return getEndTime(c.getTime());
    }

    /**
     * 获取当天最早时刻
     * @param date
     * @return
     */
    public static Date getFirstTime(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    /**
     * 获取当天最晚时刻
     * @param date
     * @return
     */
    public static Date getEndTime(Date date) {
        if (date == null) {
            return null;
        }
        Date dateFirstTime = getFirstTime(date);
        Calendar c = Calendar.getInstance();
        c.setTime(dateFirstTime);
        c.add(Calendar.DATE, 1);
        c.add(Calendar.MILLISECOND, -1);
        return c.getTime();
    }

    /**
     * 将指定日期时间月份(date)加上指定数(time)的开始时间
     * @param date
     * @param time
     * @return
     */
    public static Date MonthAdd(Date date,int field, int time) {
        // 创建Calendar实例
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // 在当前时间基础上加上time
        calendar.add(field, time);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得前一天最后时间
     */
    public static Date getDateEnd(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)-1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }


    /**
     * 计算两个时间的天数
     * @param staDate  开始时间
     * @param endDate  结束时间
     * @return
     */
    public static int getDateDayNum(Date staDate,Date endDate){
        long time = staDate.getTime();
        long time1 = endDate.getTime();
        long num = time1 - time;
        long ti = num / 24 / 60 / 60 / 1000;
        return (int)ti;
    }

}
